<?php 
@session_start();
?>
<!DOCTYPE html>
<html lang="en">
<?php	
	require_once('execute/inc/config.inc');
	include 'pagination/ps_pagination.php';
	
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
						if(!$link) {
							die('Failed to connect to server: ' . mysql_error());
						}
						
						//Select database
						$db = mysql_select_db(DB_DATABASE);
						if(!$db) {
							die("Unable to select database");
						}
	$query = "SELECT n.id AS news_id, n.title,n.content,n.publish_date,n.image,m.firstname,m.lastname,c.name 
						 FROM news n JOIN perm_members m
						 ON m.id = n.publisher_id
						 JOIN news_categories c
						 ON c.id = n.category_id
						 ORDER BY n.publish_date desc";
	$select_news=mysql_query($query);
	
	?>
<script type = "text/javascript">
<?php if(isset($_GET['search-key']))
{
?>
$(function() {	
	//call the function onload
	getdata(1);
});

function getdata( pageno ){	
	var targetURL = 'archive.php?page=' + pageno+'&search-by=<?php echo $_GET['search-by']; ?>&search-key=<?php echo $_GET['search-key']; ?>';
	document.location.href = targetURL;
}
<?php }
else {?>
$(function() {	
	//call the function onload
	getdata(1);
});

function getdata( pageno ){	
	var targetURL = 'archive.php?page=' + pageno;
	document.location.href = targetURL;
}
<?php 
}
 
 
 
 if(isset($_GET['sort-by'])) //SORTING URL CHANGING CODE
 {
 ?>
 $(function() {	
	//call the function onload
	getdata(1);
});

function getdata( pageno ){	
	var targetURL = 'archive.php?page=' + pageno+'&sort-by=<?php echo $_GET['sort-by']; ?>';
	document.location.href = targetURL;
}
<?php }
?>
</script>
<?php
	$users=mysql_query("SELECT * FROM perm_members");
	$select_more_news=mysql_query("SELECT * FROM news order by publish_date DESC limit 0,10");
?>

  <head>
    <meta charset="utf-8">
    <title>Recent News</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/bootstrap-responsive.min.css" rel="stylesheet">
    <link href="assets/css/font-awesome.css" rel="stylesheet">
    <script src="assets/js/jquery.js"></script>
    <script src="assets/js/jquery-1.8.0.min.js"></script>
    <script src="assets/js/bootstrap-transition.js"></script>
    <script src="assets/js/bootstrap-alert.js"></script>
    <script src="assets/js/bootstrap-modal.js"></script>
    <script src="assets/js/bootstrap-dropdown.js"></script>
    <script src="assets/js/bootstrap-scrollspy.js"></script>
    <script src="assets/js/bootstrap-tab.js"></script>
    <script src="assets/js/bootstrap-tooltip.js"></script>
    <script src="assets/js/bootstrap-popover.js"></script>
    <script src="assets/js/bootstrap-button.js"></script>
    <script src="assets/js/bootstrap-collapse.js"></script>
    <script src="assets/js/bootstrap-carousel.js"></script>
    <script src="assets/js/bootstrap-typeahead.js"></script>
    <style type="text/css">
      body {
        padding-top: 20px;
        padding-bottom: 40px;
      }

      /* Custom container */
      .container-narrow {
        margin: 0 auto;
        max-width: 900px;
      }
      .container-narrow > hr {
        margin: 30px 0;
      }

      /* Main marketing message and sign up button */
      .jumbotron {
        margin: 60px 0;
        text-align: center;
      }
      .jumbotron h1 {
        font-size: 72px;
        line-height: 1;
      }
      .jumbotron .btn {
        font-size: 21px;
        padding: 14px 24px;
      }

      /* Supporting marketing content */
      .marketing {
        margin: 60px 0;
      }
      .marketing p + h4 {
        margin-top: 28px;
      }
    </style>
    <link href="../assets/css/bootstrap-responsive.css" rel="stylesheet">

    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="../assets/js/html5shiv.js"></script>
    <![endif]-->
    
<script type="text/javascript">
function closeAlert(){
	var targetURL = 'archive.php';
	document.location.href = targetURL;
}
</script>
<script type="text/javascript">
$(function(){
	$("#sort-by").live("change",function(){
		var sort = $("#sort-by :selected").val();
		<?php 
		if(isset($_GET['page'])){
		?>
		var targetURL = 'archive.php?page=<?php echo $_GET['page']; ?>&sort-by='+sort;
		document.location.href = targetURL;
		<?php 
		}
		else{		
		?>
		var targetURL = 'archive.php?sort-by='+sort;
		document.location.href = targetURL;
		<?php 
		}
		?>
	});
});
</script>
    
  </head>

  <body>

    <div class="container">

    <div class="navbar navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
     <ul class="nav">
          <li><a href="index.html"><i class='icon-home'></i>&nbsp;Home</a></li>
          
          <li class='active'><a href="blog.php"><i class='icon-rss'></i>&nbsp;News</a></li>
         
          <li><a href="contact.php"><i class='icon-envelope-alt'></i>&nbsp;Contact</a></li>
        </ul>
        
        <div class="btn-group pull-right" data-toggle="buttons-radio">
		  <a href="javascript:void" class="btn" data-toggle="tab">Sign in</a>
		  <a href="signup.php" class="btn btn-danger" data-toggle="tab">Sign up</a>
		  
		</div>
    </div>
  </div>
</div>
   
	<div class='row-fluid span12' style='margin-top:43px'>
    <div class='span8'>
    <?php 
    if(isset($_GET['cat_id'])){
	$cat_head = mysql_query("SELECT * FROM news_categories where id=$_GET[cat_id]");
	if($cat_head){
	$cat_head_row = mysql_fetch_array($cat_head);
	
    ?>
    <h1><?php echo $cat_head_row['name']; ?></h1><p><i>Latest Update - <?php echo $latest_date; ?></i></p>
    <?php
    }
    else{
    	echo mysql_error();
    } 
    }
    else {
    	?>
    	<div class="span12" style="margin:0;">
    	<h1>Archived News</h1>
    	
    	<hr/>
    <div class="span12" style="margin:0;">
    <div class="alert alert-block span8" style="padding-bottom:0;">
    <div class="span12">
    <form method="get" action="archive.php">
    <p>Search by: </p><select class="span4" type="appendedInputButtons" name="search-by">
    <option value="n.title" <?php if(isset($_GET['search-by'])){ if($_GET['search-by']=='title') echo "selected"; }?>>Title</option>
    <option value="c.name" <?php if(isset($_GET['search-by'])){ if($_GET['search-by']=='c.name') echo "selected"; }?>>Category</option>
    <option value="publish_date" <?php if(isset($_GET['search-by'])){ if($_GET['search-by']=='publish_date') echo "selected"; }?>>Date (Y-M-D)</option>
    <option value="concat(m.firstname,m.lastname)" <?php if(isset($_GET['search-by'])){ if($_GET['search-by']=='concat(m.firstname,m.lastname)') echo "selected"; }?>>Author</option>
  </select>
<div class="input-append">
  <input type="text" placeholder="Search Keyword..." id="appendedInputButtons"  name="search-key"/>
  <input class="btn btn-success" type="submit" value="Search"/>
  
</div>  
</form>
</div>
</div>
    <div class="span4 pull-right">
    <div class="alert alert-block span12" style="padding-bottom:0;">
    
<form method="get" action="archive.php">
    <p>Sort by: </p><select id="sort-by" class="span12" type="appendedInputButtons" name="search-by">
    <option value="date_published" <?php
    if(isset($_GET['sort-by'])){ 
    if ($_GET['sort-by']=='date_published') {
    	echo "selected"; 
    }
    }
    ?>>Most Recent</option>
    <option value="click" <?php
    if(isset($_GET['sort-by'])){ 
    if ($_GET['sort-by']=='click') {
    	echo "selected"; 
    }
    }
    ?>>Most Viewed</option>
    
  </select>
</form>
</div>
</div>    
</div>
    	<?php
    }
    ?>
    <?php if(isset($_GET['search-key'])){
    	$skey = $_GET['search-key'];
    	$sby = $_GET['search-by'];
    	$query_search = "SELECT n.id AS news_id, n.title,n.content,n.publish_date,n.image,m.firstname,m.lastname,c.name 
						 FROM news n JOIN perm_members m
						 ON m.id = n.publisher_id
						 JOIN news_categories c
						 ON c.id = n.category_id
    					 WHERE ".$sby." like '%".$skey."%' 
						  ORDER BY n.publish_date desc";
    	$search = mysql_query($query_search);
    	$nums = mysql_num_rows($search);
    	if($nums!=0){
    	?>
		<div class="span12" style="margin:0">
		<div class="alert alert-success span12"><strong>About <?php echo mysql_num_rows($search); ?> result/s found for:</strong> <?php echo $_GET['search-key']; ?><a class="close" onclick="closeAlert()" data-dismiss="alert" href="#">&times;</a></div>
		<hr>
		</div>
		<?php } else {
		echo mysql_error();
			?>
		<div class="span12" style="margin:0">
		<div class="alert alert-error span12"><strong>No results found for: </strong> <?php echo $_GET['search-key']; ?><a class="close" onclick="closeAlert()" data-dismiss="alert" href="#">&times;</a></div>
				<hr>
		</div>
		<?php } ?>
		<hr/>
		<?php } ?>
    <hr/>
     
     
</div>

    <?php
    if(isset($search)){
    if(mysql_num_rows($search)!=0){
    $pager = new PS_Pagination ($link, $query_search, 2 , 3);
    $rs = $pager->paginate();
	}
    }
    else{
    	$pager = new PS_Pagination ($link, $query, 2 , 3);
}
	if(isset($pager)){
	$rs = $pager->paginate();
    while($news_row=mysql_fetch_object($rs)){
    	$news_id=$news_row->news_id;
    	$photo_name=$news_row->image;
    	$title=$news_row->title;
    	$text=substr($news_row->content, 0,200);
    	$text.='...';
    	$dt=$news_row->publish_date;
    	$fname = $news_row->firstname;
    	$lname = $news_row->lastname;
    	$author = $fname. " ".$lname;
    	
    	?>
 	
 	<div class="row-fluid">
 	
  <div class="span12">
    <div class="row">
      <div class="span12">
      
        <div class="span8"><h4 style="margin-left: 30px"><strong><a href="details.php?id=<?php echo $news_id; ?>"><?php echo $title; ?></a></strong></h4></div>
        <div class="span4" style="margin-left:0;"><p class="pull-right muted" style="margin-top:10px;"><small><i class="icon-user"></i> by <a href="#"><?php echo $author; ?></a> | <i class="icon-calendar"></i> <?php echo date("j-F-Y",strtotime($dt)); ?></small></p></div>
      </div>
    </div>
    <div class="row-fluid">
      <div class="span4">
        <a href="details.php?id=<?php echo $news_id; ?>">
            <img src="uploads/<?php echo $photo_name; ?>" class='span12 img-polaroid' style="height: 120px;">
        </a>
      </div>
      <div class="span8" style="margin-top:1px; margin-left:10px;">      
          <p class='muted wordwrap'><small><?php echo $text; ?></small></p>
        <a style="margin-top: 0;" class="btn btn-small" href="details.php?id=<?php echo $news_id; ?>">Read more...</a>
      </div>
    </div>
  </div>
</div>
<hr/>


 	
    <?php 
    }
    
    ?>
   <div class="pagination">
   <?php   
   echo $pager->renderFullNav();
   ?>
   </div>
   
   
   <?php }?>
   </div> 
    <div class='span4 pull-right' style="margin-top:20px;">
	<div class="sidebar-nav">
	<div class="well">
		<ul class="nav nav-list">
		  <li class="nav-header">Categories</li>
		  <div class="divider"></div>
		            <li><a href="archive.php"><i class="icon-tags"></i>&nbsp; General &nbsp;</a></li>          
		  <?php 
		$cat_select = mysql_query("SELECT * FROM news_categories order by name asc");
		while ($cat_row = mysql_fetch_array($cat_select)){
		$news_qry = mysql_query("SELECT * FROM news where category_id='$cat_row[id]'");
		if (mysql_num_rows($news_qry)>0){
		?>       
          <li><a href="categories.php?cat_id=<?php echo $cat_row['id']; ?>"><i class="icon-tags"></i>&nbsp; <?php echo $cat_row['name']; ?> &nbsp; <span class="badge badge-info"><?php echo mysql_num_rows($news_qry); ?></span></a></li>          
		<?php
		}
		
		}
		?>
		</ul>
	</div>
</div>

<div class="sidebar-nav">
	<div class="well">
		<ul class="nav nav-list"> 
		<li class="nav-header">Latest Update</li> 
		
 
		<li class="divider"></li>
		  <?php 
		  while($row_more_news=mysql_fetch_array($select_more_news)){
		  	
		  	?> 
		  	<li><a href="details.php?id=<?php echo $row_more_news['id'];?>"><i class="icon-bookmark"></i> <?php echo $row_more_news['title']; ?></a></li>
		  <?php 	
		  }
		  ?>
          
		</ul>
	</div>
</div>
   </div>
</div>
     
    </div> <!-- /container -->
    <hr>
    <div class="container">	
<div class="row-fluid">
				<div class="span12">
				
				<div class='span5'>
					<address>
						  <strong>TechNation, Afghanistan</strong><br>
						  Shahid Abdul Haq Square 
							Dunya Plaza<br/>
							Ground Floor <br>
						  Kabul, Afghanistan<br>
						  <abbr title="Phone">Phone:</abbr> (+93) (0) 70 616 9995<br/>
						  <abbr title="Email">Email:</abbr> <a href="mailto:info@technation.af">info@technation.af</a><br/>
						</address>
					</div>
					
					
					<div class="span7">
						<h3>Get Social: <a href='#'><i class='icon-facebook-sign'></i></a>&nbsp;<a href='#'><i class='icon-linkedin-sign'></i></a>&nbsp;<a href='#'><i class='icon-twitter-sign'></i></a>&nbsp;<a href='#'><i class='icon-google-plus-sign'></i></a></h3>
						  <h2>Newsletter</h2>
						    
						  <p>Subscribe to our weekly Newsletter and stay tuned.</p>
						    
						 <form class="form-search">
					        <div class="input-append">
					            <input type="text">
					            <button type="submit" class="btn btn-primary">Subscribe Now</button>
					        </div>
					    </form>
						
					</div>
					
					
					
					
					
									
				</div>
			</div>
			<hr>
			<div class="row-fluid">
				<div class="span12">
					<div class="span8">
						<ul class='nav nav-pills'>
						<li><a href="#">Terms of Service</a> </li>   
						<li><a href="#">Privacy</a> </li>   
						<li><a href="#">Security</a></li>
						
						</ul>
					</div>
					<div class="span4">
						<p class="muted pull-right">&copy; 2013 TECHNATION. All rights reserved</p>
					</div>
				</div>
			</div>
  </div>

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
   

  </body>
</html>
